package com.example.zookeepertest.config;



import lombok.Data;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

/**
 * @Author: ZhaoJie
 * @Date: 2022/3/30
 * @Description :
 * @Modify :
 * ===================================================
 * Modifier        Modifytime        Description
 * ===================================================
 */
@Configurable
@Data
public class ZookeeperConfig {
    @Value("${zookeeper.address}")
    private String connect;

    @Value("${zookeeper.sessionTimeoutMs}")
    private Integer sessionTimeoutMs;

    @Value("${zookeeper.connectionTimeoutMs}")
    private Integer connectionTimeoutMs;

    @Bean
    public CuratorFramework Client(){
        //重试策略
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
        CuratorFramework ZkClient = CuratorFrameworkFactory.builder()
                .connectString(connect)
                .sessionTimeoutMs(sessionTimeoutMs)
                .connectionTimeoutMs(connectionTimeoutMs)
                .retryPolicy(retryPolicy)
                .namespace("") //根节点
                .build();
        ZkClient.start();
        return ZkClient;
    }

}
